package modelLayer;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * This class is part of WesternStyle sales management system. 
 * It holds information on an invoice.
 * 
 * @author A. Kelemen (Design)
 * 		   ...	(Review)
 * 		   ...	(Test)
 * @Version 1.1
 * 			
 */

public class Invoice {
	
	private int 			 invoiceID;
	private Date 			 paymentDate;
	private double 			 total;
	private List<SalesOrder> salesOrders;
	
	/**
	 * Constructs an instance of Invoice.
	 * 
	 * This constructor takes no parameters.
	 */
	public Invoice() {
		
		
		
	}
	
	/**
	 * Constructs an instance of Invoice. This constructor controls the value of the parameter
	 * passed down from the user-interface. If the values doesn't comply with the
	 * standards set forth, an exception will be thrown, and afterwards caught in the uiLayer,
	 * describing the problem to the user.
	 * 
	 * @param invoiceID		int		ID of a given invoice created in the database.
	 */
	public Invoice(int invoiceID) {
		
		this.setPaymentDate();
		if (!this.setInvoiceID(invoiceID)) {
			throw new IllegalArgumentException("Der opstod en fejl i oprettelsen af invoice id.");
		}

	}

	/**
	 * Returns the ID of an given invoice object.
	 * @return the invoiceID	
	 */
	public int getInvoiceID() {
		
		return this.invoiceID;
		
	}

	/**
	 * Sets the value of invoiceID. Controls the value if it's a number greater than 0.
	 * 
	 * Pre:		Parameter invoiceID is defined and passed over by the caller. Invoice object is under construction.
	 * Post:	a.	Method assigns invoiceID to the field invoiceID and returns true.
	 * 			b.	Method returns false if the invoiceID is not a number greater than 0.
	 * 
	 * @param invoiceID the invoiceID to set
	 */
	public boolean setInvoiceID(int invoiceID) {
		
		if (invoiceID > 0 ) {
			this.invoiceID = invoiceID;
			return true;
		}
		else {
			return false;
		}
		
	}

	/**
	 * Returns the value of payment Date.
	 * 
	 * @return the paymentDate	
	 */
	public Date getPaymentDate() {
		
		return this.paymentDate;
		
	}

	/**
	 * Sets the value of the date to the current date (dd-mm-yyyy-hour-min-sec).
	 * 
	 * @param paymentDate the paymentDate to set
	 */
	public void setPaymentDate() {
		
		this.paymentDate = new Date();
		
	}

	/**
	 * Returns the total sum on the invoice.
	 * 
	 * @return the total	
	 */
	public double getTotal() {
		
		return this.total;
		
	}

	/**
	 * Sets the value of total.
	 * 
	 * @param total the total to set
	 */
	public void setTotal(double total) {
		
		this.total = total;
		//TODO write a method which calculates the total
		
	}

	/**
	 * Returns a copy of salesOrders. 
	 * Controls if the length of the list salesOrders is greater than 0.
	 * 
	 * @return the salesOrders
	 */
	public List<SalesOrder> getSalesOrders() {
		
		List<SalesOrder> orders = new ArrayList<>();
		if (salesOrders.size() > 0) {
			orders = this.salesOrders;
		}
		return orders;
		
	}

	/**
	 * Adds a salesOrder object to the salesOrders list.
	 * Controls if the salesOrder object is not null and if the list already contains the object to be added.
	 * 
	 * @param salesOrders the salesOrders to set
	 */
	public void addSalesOrders(SalesOrder salesOrder) {
		
		if (salesOrder != null && !salesOrders.contains(salesOrder)) {
			this.salesOrders.add(salesOrder);
		}

	}

}
